import types from './types'
import { ApiGetLoginUser, ApiLogout, ApiGetUserInfoBySn } from '@api/user'

const mockGetLoginUser = appId =>
  new Promise(resolve => {
    setTimeout(() => {
      resolve({
        status: true,
        code: 200,
        message: null,
        data: {
          sn: 'admin',
          name: '数据中心管理员',
          indexUrl: './appcenter/',
          roles: [{ id: 1, name: 'sss', code: null }]
        },
        otherData: {}
      })
    }, 0)
  })
const mockGetUserInfoBySn = sn =>
  new Promise(resolve => {
    setTimeout(() => {
      resolve({
        status: true,
        code: 0,
        message: null,
        data: {
          userName: '数据中心管理员',
          sn: 'admin',
          id: 65,
          phoneNum: '13800010001',
          email: '13800010001@qq.com',
          userType: '数据中心管理员',
          gender: null,
          birthday: 1430470167000,
          age: 4,
          nativePlace: null,
          status: 1,
          jobList: [
            {
              id: 3,
              name: '成员',
              code: '000001',
              isSys: 1,
              remarks: '成员',
              createBy: 'sys',
              createDate: 1558669620000,
              updateBy: 'test',
              updateDate: 1559223569000,
              status: 1,
              startDate: 1558324026000,
              endDate: 3768298769000,
              organizationCode: null
            }
          ],
          roleList: [
            {
              id: 18,
              name: '超级管理员',
              sort: 101,
              status: 1,
              createBy: 'test',
              createDate: 1559282896000,
              updateBy: 'test',
              updateDate: 1559283012000,
              remarks: '最高权限',
              code: '000008'
            },
            {
              id: 21,
              name: '数据中心管理员',
              sort: 3,
              status: 1,
              createBy: 'test',
              createDate: 1559352639000,
              updateBy: 'test',
              updateDate: 1559352646000,
              remarks: '数据中心管理员（勿删）',
              code: '000011'
            }
          ],
          organizationList: [
            {
              id: 89,
              name: '学校',
              code: '000014',
              parentCode: null,
              typeCode: '教学组织',
              level: 1,
              principalSn: null,
              status: 1,
              remarks: '',
              createBy: 'test',
              createDate: 1559198469000,
              updateBy: null,
              updateDate: null,
              startDate: 1559198469000,
              endDate: 2190350469000,
              childrenList: null,
              queryDate: null,
              userJobList: [
                {
                  id: 3,
                  name: '成员',
                  code: '000001',
                  isSys: 1,
                  remarks: '成员',
                  createBy: 'sys',
                  createDate: 1558669620000,
                  updateBy: 'test',
                  updateDate: 1559223569000,
                  status: 1,
                  startDate: 1558324026000,
                  endDate: 3768298769000,
                  organizationCode: null
                }
              ]
            },
            {
              id: 97,
              name: '董事会',
              code: '000018',
              parentCode: null,
              typeCode: '行政组织',
              level: 1,
              principalSn: null,
              status: 1,
              remarks: '',
              createBy: 'test',
              createDate: 1559208592000,
              updateBy: null,
              updateDate: null,
              startDate: 1559208592000,
              endDate: 2190360592000,
              childrenList: null,
              queryDate: null,
              userJobList: [
                {
                  id: 3,
                  name: '成员',
                  code: '000001',
                  isSys: 1,
                  remarks: '成员',
                  createBy: 'sys',
                  createDate: 1558669620000,
                  updateBy: 'test',
                  updateDate: 1559223569000,
                  status: 1,
                  startDate: 1558324026000,
                  endDate: 3768298769000,
                  organizationCode: null
                }
              ]
            }
          ],
          queryDate: 1587903336019,
          dbSourceList: null
        },
        otherData: {}
      })
    }, 0)
  })
const mockLogout = () =>
  new Promise(resolve => {
    setTimeout(() => {
      resolve({
        status: true,
        code: 200,
        message: null,
        data: null,
        otherData: {}
      })
    }, 0)
  })

export default {
  // 获取用户登录信息
  async fetchLoginUser({ dispatch }, appId) {
    // let res = await ApiGetLoginUser(appId)
    let res = await mockGetLoginUser(appId)
    if (res.status && res.data) {
      dispatch('setSn', res.data.sn)
      dispatch('setLoginUser', res.data)
      dispatch(
        'setCurrentRole',
        (res.data.roles &&
          Array.isArray(res.data.roles) &&
          res.data.roles.length &&
          res.data.roles[0].code) ||
          null
      )
    }
  },
  // 根据sn查用户信息
  async fetchUserInfoBySn({ dispatch }, sn) {
    // let res = await ApiGetUserInfoBySn(sn)
    let res = await mockGetUserInfoBySn(sn)
    if (res.status && res.data) {
      dispatch('setUserInfo', res.data)
    }
  },
  // 退出
  async logout({ dispatch }) {
    // let res = await ApiLogout()
    let res = await mockLogout()
    if (res.status) {
      dispatch('initUser')
      // TODO: 编写退出的逻辑
    }
  },
  setSn({ commit }, sn) {
    commit(types.SET_SN, sn)
  },
  setLoginUser({ commit }, loginUser) {
    commit(types.SET_LOGIN_USER, loginUser)
  },
  setCurrentRole({ commit }, role) {
    commit(types.SET_ROLE, role)
  },
  setUserInfo({ commit }, userInfo) {
    commit(types.SET_USER_INFO, userInfo)
  },
  initUser({ commit }) {
    commit(types.INIT_USER)
  }
}
